package office.service.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.business.form.BidBookForm;
import office.action.business.form.TenderCompanyConditionForm;
import office.action.business.form.TenderCompanyForm;
import office.action.business.wrapper.TenderCompanyWrapper;
import office.bo.business.impl.TenderCompany;
import office.domainset.BizDomainProviderSetImpl;
import office.service.business.IBidBookService;
import office.service.business.ITenderCompanyService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class TenderCompanyServiceImpl extends BaseBizServiceImpl implements ITenderCompanyService{
	private IBidBookService bidBookService;
	public void editTenderCompany(TenderCompanyForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			TenderCompany bo = (TenderCompany)domainProviderSetImpl.getTenderCompanyDP().newEmptyObj();
			bo =(TenderCompany) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑产品明细失败!", e);
		}
	}
	
	public Page getPageInfo(TenderCompanyConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getTenderCompanyDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "TenderCompany.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getCompanyName())){
				condMap.put("companyName", condition.getCompanyName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findTenderCompanyByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<TenderCompanyWrapper> findAllTenderCompany(TenderCompanyConditionForm condition,Page page)throws BusinessException{
		List<TenderCompanyWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getTenderCompanyDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getCompanyName())){
				condMap.put("companyName", condition.getCompanyName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findTenderCompanyByIdAndName",condMap,new HashMap(),page);
			TenderCompanyWrapper wrapper;
			for(int i=0;i<list.size();i++){
				TenderCompany bo = (TenderCompany)list.get(i);
				wrapper = new TenderCompanyWrapper();
				if(bo.getBidBookId()!=null){
					BidBookForm bidBook=bidBookService.getById(bo.getBidBookId());
					bo.setBidBookName(bidBook.getProjectName());
				}
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取产品明细列表失败！", e);
		}
		return wrapperList;
	}

	public void delTenderCompanyById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getTenderCompanyDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除产品明细失败！", e);
		}
	}
	
	public TenderCompanyForm getById(String id)throws BusinessException{
		TenderCompanyForm form = new TenderCompanyForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			TenderCompany bo = (TenderCompany)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getTenderCompanyDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取产品明细失败!", e);
		}
		return form;
	}
	/**
	 * 根据标书的ID获取标书对应的竞标公司
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public List<TenderCompanyWrapper> findAllTenderCompanyByBidBookId(TenderCompanyConditionForm condition)throws BusinessException{
		List<TenderCompanyWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getTenderCompanyDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getBidBookId())){
				condMap.put("bidBookId", condition.getBidBookId());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findTenderCompanyByBidBookId",condMap,new HashMap());
			TenderCompanyWrapper wrapper;
			for(int i=0;i<list.size();i++){
				TenderCompany bo = (TenderCompany)list.get(i);
				wrapper = new TenderCompanyWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据标书获取竞标公司列表失败！", e);
		}
		return wrapperList;
	}

	public IBidBookService getBidBookService() {
		return bidBookService;
	}

	public void setBidBookService(IBidBookService bidBookService) {
		this.bidBookService = bidBookService;
	}
	
}
